package recall;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

// 全排列
public class leetcode46 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        List<List<Integer>> results = permute(nums);
//        for(int i = 0; i < results.size(); i++){
//            List<Integer> array = results.get(i);
//            for(int j = 0; j < array.size(); j++){
//                System.out.print(results.get(j) + ",\t");
//            }
//            System.out.println();
//        }
        for(int i = 0; i < results.size(); i++){
            System.out.println(results.get(i));
        }
    }

    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> results = new ArrayList<>();
        boolean[] visited = new boolean[nums.length];
        BackTracking(nums, visited, new ArrayList<>(), results);

        return results;
    }

    public static void BackTracking(int[] nums, boolean[] visited, List<Integer> path, List<List<Integer>> result){
        if(path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i++){
            if(visited[i] == false){
                visited[i] = true;
                path.add(nums[i]);
                BackTracking(nums, visited, path, result);
                visited[i] = false;
                path.removeLast();
            }
        }
    }
}
